<?php 
$req_path="../";
include($req_path."includes/ini.php");
include($req_path."includes/db.php");

class M_master extends DB{
    private $rowCount=0;
    public $table_name="";
    function M_master(){
		parent::__construct();
	//$this->dbConn=$this->getDB();
    }
    function setTable($tb_name){
		$this->table_name = "tb_".$tb_name;
	}
    function loadData(){
		$sqlscript= "SELECT id, name ,status ";
        $sqlscript.= "FROM ". $this->table_name;
		
		if ($this->table_name == 'tb_income_list')
			$sqlscript.= " WHERE delete_flage ='Y' ";
        
        $result = $this->ExecuteData($sqlscript);
        $this->rowCount = $result->RecordCount();
        return $result;
    }
	function loadFee(){
		$sqlscript= "SELECT id, DATE_FORMAT(effective_date , '%d/%m/%Y' ) effect_date ,term ,assets ";
        $sqlscript.= "FROM tb_master_fee ";
		$sqlscript.=" order by effective_date desc";
        
        $result = $this->ExecuteData($sqlscript);
        $this->rowCount = $result->RecordCount();
        return $result;
	}
	function loadAge(){
		$sqlscript= "SELECT DATE_FORMAT(age_date , '%d-%m' ) age_date , DATE_FORMAT(age.createdate , '%d/%m/%Y' ) createdate , u.username  ";
        $sqlscript.= "FROM tb_master_age age ";
		$sqlscript.=" inner join tb_user u on u.id = age.createby  order by age.createdate desc";
        
        $result = $this->ExecuteData($sqlscript);
        $this->rowCount = $result->RecordCount();
        return $result;
	}
	
	function load_type(){
		$sqlscript= " SELECT id, name ,status ";
		$sqlscript.= " From tb_product_type ";
		
		$result = $this->ExecuteData($sqlscript);
        $this->rowCount = $result->RecordCount();
        return $result;
	}
	function loadSupplier(){
		$sqlscript= " SELECT id, name,address,telephone ,status ";
		$sqlscript.= " From tb_supplier ";
		
		$result = $this->ExecuteData($sqlscript);
        $this->rowCount = $result->RecordCount();
        return $result;
	}
	function loadProduct($name='',$type=0){
		
		$sqlscript= " SELECT p.code, p.id, p.name, t.name as type_name , cost , price , p.status ";
		$sqlscript.= " From tb_product p join tb_product_type t on t.id = p.type " ;
		$sqlscript.= " Where 1=1 ";
		if ($name!=''){
			$sqlscript.= " and p.name like '%".$name."%'";
		}
		if ($type !=0){
			$sqlscript.=" and p.type=".$type;
		}
		$sqlscript.= " order by p.name , p.type ";
		//;;print_r($sqlscript);
		$result = $this->ExecuteData($sqlscript);
        $this->rowCount = $result->RecordCount();
		
        return $result;
	}
	function checkProductCode($code){
		$sql = "SELECT * FROM tb_product where code='".$code."'";
	 
		$result = $this->ExecuteData($sql);
        return $result->RecordCount();
	}
    function getRowCount(){
        
        return $this->rowCount;
        
    }	
    function insertData($val){
            if (isset($val)){
				$sqlInsert=$this->InsertSqlScript("SELECT name,status , createdate, createby FROM ". $this->table_name." where 1=2 ", $val);
				$this->ExecuteData($sqlInsert);
            }
    }
	function insertDataWithSql($sql , $val){
			if (isset($val)){
				$sqlInsert=$this->InsertSqlScript($sql, $val);
				//print_r($sqlInsert);
				$this->ExecuteData($sqlInsert);
            }
	}
    function updateData($id,$val){
            if(isset($val)){
				$sqlUpdate=$this->UpdateSqlScript("SELECT name,status , updatedate, updateby FROM ". $this->table_name." where id='".$id."'" ,$val);
				 $this->ExecuteData($sqlUpdate);
				// return $sqlUpdate;
            }
    }function updateDataWithSql( $sql , $val){
		  if(isset($val)){
				$sqlUpdate=$this->UpdateSqlScript($sql ,$val);
				 $this->ExecuteData($sqlUpdate);
				// return $sqlUpdate;
            }
	
	}
    function deleData($id){
            if(isset($id)){
			$sqlDelete = "Delete FROM ". $this->table_name." where id='".$id."'";
			$this->ExecuteData($sqlDelete);
			return 1;
            }
    }
	function getType(){
	 
		$sqlscript= "select id, name from tb_product_type where status = 'Y' Order by id asc ";
		
		$result = $this->ExecuteData($sqlscript);
		while($row = $result->FetchRow()){
			$ar[$row["name"]] = $row["id"];
		}
		return $ar;	
	
	}
    
}
?>
